package com.chen.im.view;

import com.chen.common.FX;
import com.chen.boot.config.GlobalListener;
import com.chen.boot.config.GlobalType;
import com.chen.boot.db.entity.ContactsInfo;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.UUID;

import de.felixroske.jfxsupport.FXMLController;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.control.MultipleSelectionModel;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import lombok.Getter;

/**
 * @Description
 * @Author Pengwei
 * @CreatDate 2021/11/14 14:18
 */
@Getter
@FXMLController
public class ContactsList implements Initializable {

    public static ContactsList instance;

    public static ObservableList<ContactsInfo> items = FXCollections.observableArrayList();

    public static HashMap<String, ContactstListItem> hashMap = new HashMap<>();

    @FXML
    ListView<ContactsInfo> contactsListView;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        this.instance = this;
        contactsListView.setItems(items);
        //切换联系人监听
        listenerChangeContacts();
        //设置自定义Item
        setContactsListView();
    }

    private void listenerChangeContacts() {
        contactsListView.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            ChatEdit.instance.getEditText().setText("");
        });
    }

    //设置自定义Item
    private void setContactsListView() {
        contactsListView.setCellFactory(param -> new ListCell<ContactsInfo>() {
            @Override
            protected void updateItem(ContactsInfo item, boolean empty) {
                Platform.runLater(() -> {
                    super.updateItem(item, empty);
                    //如果数据为空,setGraphic(null),避免重复数据
                    if (empty || item == null) {
                        setGraphic(null); return;
                    }
                    FXMLLoader fxmlLoader = FX.getFXMLLoader("/view/ContactsList_item.fxml");
                    HBox hBox = null;
                    try {
                        hBox = fxmlLoader.load();
                        hBox.setPrefWidth(0); //重要,不设置此项item会超出屏幕,形成滑动bar
                        this.setGraphic(hBox);
                        ContactstListItem controller = fxmlLoader.getController();
                        //给每个ITEM绑定一个uuid,并放进map中,方便后续查找该UUID
                        String uuid = UUID.randomUUID().toString();
                        item.setControllerUUID(uuid);
                        hashMap.put(uuid, controller.setUuid(uuid).setData(item));
                    } catch (IOException e) {
                        setGraphic(null);
                        e.printStackTrace();
                    }
                });

            }
        });
    }

    public static void updateItems(ContactsInfo contactsInfo) {
        ContactsInfo viewItem = items.stream().filter(
                item -> item.getId().equals(contactsInfo.getId())
                        && item.getIp().equals(contactsInfo.getIp())
        ).findAny().orElse(null);

        //ID相同,IP相同,则不作处理
        if (viewItem != null) {
            updateName(viewItem, contactsInfo.getName());
            return;
        }
        items.add(contactsInfo);
    }

    //更新用户显示名称
    public static void updateName(ContactsInfo viewItem, String name) {
        if (viewItem.getName().equals(name)) {
            return;
        }
        viewItem.setName(name);
        ContactstListItem contactstListItem = hashMap.get(viewItem.getControllerUUID());
        contactstListItem.setData(viewItem);
        GlobalListener.instance.listenerEvent(GlobalType.contactsUpdateName, viewItem);
    }

    //更新用户别名
    public static void updateAlias(ContactsInfo viewItem, String alias) {
        viewItem.setAlias(alias);
        ContactstListItem contactstListItem = hashMap.get(viewItem.getControllerUUID());
        contactstListItem.setData(viewItem);
        GlobalListener.instance.listenerEvent(GlobalType.contactsAlias, viewItem);
    }
}
